package com.lc.recovery.ui.activity.recoverymine;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.load.MultiTransformation;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.recovery.R;
import com.lc.recovery.app.AppActivity;
import com.lc.recovery.http.glide.GlideApp;
import com.lc.recovery.http.request.RankListApi;
import com.lc.recovery.http.request.RankTypeApi;
import com.lc.recovery.http.response.RankListBean;
import com.lc.recovery.http.response.RankTypeBean;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;

public class WinningActivity extends AppActivity {
    @BindView(R.id.rv_recoveryshop)
    RecyclerView rvRecoveryshop;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private BaseQuickAdapter<RankTypeBean.DataBean, BaseViewHolder> shopstopAdapter;
    private BaseQuickAdapter<RankListBean.DataBean, BaseViewHolder> adapter;
    private List<RankTypeBean.DataBean> datas=new ArrayList<>();
    private List<RankListBean.DataBean> datalist=new ArrayList<>();
    @Override
    protected int getLayoutId() {
        return R.layout.activity_winning;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        shopstopAdapter = new BaseQuickAdapter<RankTypeBean.DataBean, BaseViewHolder>
                (R.layout.item_recovery_shopnew, datas) {

            @Override
            protected void convert(BaseViewHolder baseViewHolder, RankTypeBean.DataBean dataBean) {
                baseViewHolder.setText(R.id.tv_name, dataBean.getTitle());
                if (dataBean.isIsgrop()) {
                    baseViewHolder.setBackgroundResource(R.id.ll_bg, R.color.color01B467);

                }else {
                    baseViewHolder.setBackgroundResource(R.id.ll_bg, R.color.colorf5f5f5);
                }

            }
        };

        shopstopAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {

                for (RankTypeBean.DataBean bean :datas){
                    bean.setIsgrop(false);
                }
                RankTypeBean.DataBean bean = datas.get(position);
                bean.setIsgrop(!bean.isIsgrop());
                shopstopAdapter.notifyDataSetChanged();
                rank_list(datas.get(position).getId()+"");

            }
        });

        rvRecoveryshop.setAdapter(shopstopAdapter);
        adapter = new BaseQuickAdapter<RankListBean.DataBean, BaseViewHolder>
                (R.layout.item_winning, datalist) {

            @Override
            protected void convert(BaseViewHolder helper, RankListBean.DataBean dataBean) {

                if (helper.getPosition()==0) {
                    helper.setBackgroundResource(R.id.tv_number, R.mipmap.num1);
                    helper.setText(R.id.tv_number, "");
                }else if (helper.getPosition()==1){
                    helper.setBackgroundResource(R.id.tv_number, R.mipmap.num2);
                    helper.setText(R.id.tv_number, "");
                }else if (helper.getPosition()==2) {
                    helper.setText(R.id.tv_number, "");
                    helper.setBackgroundResource(R.id.tv_number, R.mipmap.num3);
                }else {
                    helper.setText(R.id.tv_number, helper.getAbsoluteAdapterPosition()+1);
                }
                GlideApp.with(getActivity())
                        .load(dataBean.getAvatar())
                        .placeholder(R.mipmap.avatar_placeholder_ic)
                        .error(R.mipmap.avatar_placeholder_ic)
                        .transform(new MultiTransformation<>(new CenterCrop(), new CircleCrop()))
                        .into((ImageView) helper.getView(R.id.img_head));
                helper.setText(R.id.tv_money,dataBean.getWeights()+"kg");
                helper.setText(R.id.tv_user,dataBean.getReal_name());


            }
        };
        View emptyview1 = View.inflate(getContext(), R.layout.empty_emptylayout, null);
        adapter.setEmptyView(emptyview1);
        recyclerView.setAdapter(adapter);
        rank_type();
//        rank_list();
    }

    private void rank_type() {
        EasyHttp.post(this)
                .api(new RankTypeApi()).request(new HttpCallback<RankTypeBean>(this) {

            @Override
            public void onSucceed(RankTypeBean data) {
                if (data.getCode() == 200) {
                    datas.clear();
                    datas.addAll(data.getData());
                    shopstopAdapter.setNewData(datas);
                    shopstopAdapter.notifyDataSetChanged();
                    if (datas.size()>0){
                        datas.get(0).setIsgrop(true);
                    }
                    rank_list(datas.get(0).getId()+"");
                } else {
                    toast(data.getMsg());
                }
            }
        });
    }

    private void rank_list(String type) {
        EasyHttp.post(this)
                .api(new RankListApi().setType(type)).request(new HttpCallback<RankListBean>(this) {

            @Override
            public void onSucceed(RankListBean data) {
                if (data.getCode() == 200) {
                    datalist.clear();
                    datalist.addAll(data.getData());
                    adapter.setNewData(datalist);
                    adapter.notifyDataSetChanged();

                } else {
                    toast(data.getMsg());
                }
            }
        });
        }
}
